//
//  CellBottomView.m
//  YSSmartHome
//
//  Created by yanghao on 2017/1/10.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "CellBottomView.h"
#import "YHMarcros.h"

@interface CellBottomView ()
@property(nonatomic, strong)UIButton *editBtn;
@property(nonatomic, strong)UIButton *delBtn;
@end
@implementation CellBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self initSubviews];
    }
    return self;
}

#pragma mark - event response
- (void)btnClick:(UIButton*)btn
{
    CellBottomViewClickType clickType = CellBottomViewClickTypeEdit;
    if (btn == self.delBtn)
    {
        clickType = CellBottomViewClickTypeDelete;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellottomView:clickType:)])
    {
        [self.delegate cellottomView:self clickType:clickType];
    }
}

#pragma marm - private method

- (void)initSubviews
{
    [self addSubview:self.editBtn];
    [self addSubview:self.delBtn];
    [self layoutPageSubviews];
}
- (void)layoutPageSubviews
{
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(self);
        make.width.equalTo(self.width).multipliedBy(0.5);
    }];
    
    [self.delBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.trailing.equalTo(self);
        make.leading.equalTo(self.editBtn.trailing);
    }];
}
- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef contex = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(contex, color.CGColor);
    CGContextFillRect(contex, rect);
    UIImage *image =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
#pragma mark - getters and setters
- (UIButton *)editBtn
{
    if (!_editBtn)
    {
        _editBtn = [[UIButton alloc]init];
        [_editBtn setTitle:@"编辑" forState:UIControlStateNormal];
        _editBtn.titleLabel.font  = [UIFont systemFontOfSize:14];
        _editBtn.backgroundColor = [UIColor greenColor];
        [_editBtn setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
        [_editBtn setBackgroundImage:[self imageWithColor:RGBColor(129, 129,129)] forState: UIControlStateNormal];
        [_editBtn setBackgroundImage:[self imageWithColor:RGBColor(153, 153,153)] forState: UIControlStateHighlighted];
        [_editBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editBtn;
}
- (UIButton *)delBtn
{
    if (!_delBtn) {
        _delBtn = [[UIButton alloc]init];
        [_delBtn setTitle:@"删除" forState:UIControlStateNormal];
        _delBtn.titleLabel.font  = [UIFont systemFontOfSize:14];
        [_delBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_delBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
        [_delBtn setBackgroundImage:[self imageWithColor:RGBColor(129, 129,129)] forState: UIControlStateNormal];
        [_delBtn setBackgroundImage:[self imageWithColor:RGBColor(153, 153,153)] forState: UIControlStateHighlighted];
    }
    return _delBtn;
}
@end
